package life.git_one.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by life on 2017/7/16.
 */

public class MyView extends View {

    private Paint mPaint;
    private int mHeight;
    private int mWeight;
    private Paint mPaint2;
    private Paint mPaint3;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initView() {
        mPaint = new Paint();   //实心方块
        mPaint2 = new Paint();  //空心方块
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setStrokeWidth(10);
        mPaint2.setColor(Color.RED);
        mPaint3 = new Paint();
        mPaint3.setStyle(Paint.Style.FILL);
        mPaint3.setColor(Color.CYAN);
        mPaint3.setStrokeWidth(20);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = w;
        mWeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*//黑色正方形
        canvas.translate(mWeight / 2, mHeight / 2);
        Rect rect = new Rect(-200, -400, 200, 0);
        mPaint.setColor(Color.BLACK);
        canvas.drawRect(rect, mPaint);
        canvas.drawPoint(mWeight / 2,mHeight / 2, mPaint3);
        canvas.drawCircle(0, 0, 100,  mPaint3);
        canvas.save();

        //红色正方形
        mPaint.setColor(Color.RED);
        canvas.scale(0.5f, 0.5f, 200, 0);
        canvas.drawRect(rect, mPaint);

        //灰色正方形
        canvas.restore();
        canvas.save();
        mPaint.setColor(Color.BLACK);
        canvas.scale(-0.5f, -0.5f, 70, 0);
        canvas.drawRect(rect, mPaint);

        canvas.restore();
        canvas.save();
        canvas.translate(200, 400);
        canvas.scale(0.5f, 0.5f, -400, 0);
        RectF rect2 = new RectF(-400,-400,400,400);
        canvas.drawRect(rect2, mPaint2);
        for (int i = 0; i < 20; i++) {
            canvas.scale(0.9f, 0.9f);
            canvas.drawRect(rect2, mPaint2);
        }*/

        //环圆形断线
        canvas.translate(mWeight / 2 - 200, mHeight / 2 + 200);
        canvas.drawPoint(0,0,mPaint3);
        mPaint2.setColor(Color.BLACK);
        canvas.drawCircle(0, 0, 400, mPaint2);
        canvas.drawCircle(0, 0, 380, mPaint2);
        for (int i = 0; i < 360; i += 10) {
            if ((i / 10) % 2 == 0) {
                mPaint2.setColor(Color.GREEN);
            } else {
                mPaint2.setColor(Color.RED);
            }
            canvas.drawLine(0, 380, 0, 400, mPaint2);
            canvas.rotate(10);
        }

        //错切
        mPaint2.setColor(Color.BLACK);
        Rect rect4 = new Rect(0, 0, 200, 200);
        canvas.skew(1, 0);
        canvas.drawRect(rect4, mPaint2);
    }
}
